package MCQReaderSystem;

import java.lang.Runtime;
//import java.io.*;
import java.lang.Process;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;

public class Orienter {
	String[] tab_commandes;

	int point1x, point1y, point2x, point2y;

	int point3x, point3y, point4x, point4y;

	double theta;

	public Orienter(String args) throws Exception {
		String rep = "extractcircle.sh";
		String valeur = null;
		File fichier = new File(rep);
		try {
			valeur = fichier.getAbsolutePath();
			System.out.println(valeur);
		} catch (Exception ex) {
			ex.getMessage();
		}
		;
		tab_commandes = new String[3];

		tab_commandes[0] = valeur;
		tab_commandes[1] = args;
		tab_commandes[2] = System.getProperty("user.dir");

		System.out.println("Contenu du tableau des commandes:");
		for (int cpt_tab = 0; cpt_tab < tab_commandes.length; cpt_tab++)
			System.out.println("Col. " + cpt_tab + " -> "
					+ tab_commandes[cpt_tab]);

		// creation de processus pour l'execution du script
		Process processus = Runtime.getRuntime().exec(tab_commandes);
		processus.waitFor(); // permet d'attendre la fin de l'execution du
		// script

		BufferedImage img = null;
		try {
			img = ImageIO.read(new File("masscenters.bmp"));
		} catch (IOException e) {
		}
		int w = img.getWidth();
		int h = img.getHeight();
		Raster pixels = img.getRaster();
		int cpt = 1;
		for (int x = 0; x < w; x++) {
			for (int y = 0; y < h; y++) {
				int r = pixels.getSample(x, y, 0);// quantite de rouge
				int g = pixels.getSample(x, y, 1);// quantite de vert
				int b = pixels.getSample(x, y, 2);// quantite de bleu
				if (r != 0 && g != 0 && b != 0) { // test si le pixel n'est
					// noir
					if (cpt == 1) {
						point1x = x;
						point1y = y;
						System.out.println("(x1:" + point1x + ", y1:" + point1y
								+ ")");
					}
					if (cpt == 2) {
						point2x = x;
						point2y = y;
						System.out.println("(x2:" + point2x + ", y2:" + point2y
								+ ")");
					}
					if (cpt == 3) {
						point3x = x;
						point3y = y;
						System.out.println("(x3:" + point3x + ", y3:" + point3y
								+ ")");
					}

					if (cpt == 4) {
						point4x = x;
						point4y = y;
						System.out.println("(x4:" + point4x + ", y4:" + point4y
								+ ")");
					}
					// System.out.println("mass center num="+(cpt++)+ //dans ce
					// cas c'est un des pixels recherches
					// " located at ("+x+","+y+")"); //et on affiche les
					// coordonnees
					cpt++;
				}
			}
		}
		if (cpt - 1 < 4) {
			System.out.println("Veuilez positioner correctement papier");
			System.exit(0);
		}
	}

	public void orienter(String args) {
		double theta = 0;
		theta = (getPoint2x() - getPoint1x()) / (getPoint2y() - getPoint1y());

		double arctan = Math.atan(theta);
		ImagePanel img2 = new ImagePanel(args, 0, 0);
		System.out.println(theta);
		img2.rotate(arctan);
	}

	public double getPoint1x() {
		return point1x;
	}

	public double getPoint1y() {
		return point1y;
	}

	public double getPoint2x() {
		return point2x;
	}

	double getPoint2y() {
		return point2y;
	}

	double getPoint3x() {
		return point3x;
	}

	double getPoint3y() {
		return point3y;
	}

	double getPoint4x() {
		return point4x;
	}

	double getPoint4y() {
		return point4y;
	}

}